package ui.tp3.TP3Wicket;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.ContextRelativeResource;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormChoiceComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.PropertyModel;

import ui.tp3.modelo.Coordenada;
import ui.tp3.modelo.Esfera;
import ui.tp3.modelo.Tablero;
import ui.tp3.modelo.UserException;

public class CreacionEsferasPage extends WebPage {
	private static final long serialVersionUID = 2L;
	private Tablero tablero;
	private String selected = "1";
	private Image imgEsfera;
	private Esfera esfera = new Esfera();
	private FeedbackPanel feedbackPanel;
      
      public String getSelected()
      { return this.selected;}

      public void setSelected(String s)
      {  this.selected=s;}
      
    public CreacionEsferasPage(AccionesSobreElMapaPage accionesSobreElMapaPage) {
       
    	feedbackPanel = new FeedbackPanel("feedback");
		add(feedbackPanel);
		feedbackPanel.setOutputMarkupId(true);
    	
    	final AccionesSobreElMapaPage acp= accionesSobreElMapaPage;
      	this.tablero = accionesSobreElMapaPage.getTablero();
    	
    	esfera.setNumeroEstrellas(0);
    	
    	final Coordenada coord = new Coordenada();
    	coord.setX(0);
    	coord.setY(0);
  
    	final ArrayList<Integer>estrellasDisponibles= tablero.estrellasDisponibles();
    	  	 
    	Form form = new Form("formulario1");
    	form.setOutputMarkupId(true);
    	
    	final TextField<Integer> numeroColumna = new TextField<Integer>("numColumna", new PropertyModel<Integer>(this.esfera.coordenada, "x"));
    	final TextField<Integer> numeroFila = new TextField<Integer>("numFila", new PropertyModel<Integer>(this.esfera.coordenada, "y"));
    	form.add(numeroColumna);
    	form.add(numeroFila);
   
        
        RadioChoice<Integer> numeroDeEstrellas = new RadioChoice<Integer>(
    	"numEstrellas", new PropertyModel<Integer>(this, "selected"), estrellasDisponibles);
        	
        this.imgEsfera = new Image("img", new ContextRelativeResource("/"+ "Esfera " + selected +".jpg"));
        imgEsfera.setOutputMarkupId(true);
        
        form.add(this.imgEsfera);
        
        numeroDeEstrellas.add(new AjaxFormChoiceComponentUpdatingBehavior() {
			private static final long serialVersionUID = 1L;

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				CreacionEsferasPage.this.imgEsfera.setImageResource(new ContextRelativeResource("/"+ "Esfera " +CreacionEsferasPage.this.selected +".jpg"));
				target.add(CreacionEsferasPage.this.imgEsfera);
				
			}
		});
        
    	form.add(numeroDeEstrellas);   	
    	
    	form.add(new Button("volver"){
			private static final long serialVersionUID = 1L;
			
			public void onSubmit() {
				this.setResponsePage(acp);
				}
				
			});
    	
    	form.add(new FeedbackAjaxButton("crearEsfera", this.feedbackPanel) {

			protected void doOnSubmit(AjaxRequestTarget target, Form<?> form) {
					esfera.setNumeroEstrellas(Integer.parseInt(selected));
				
					tablero.agregarEsfera(esfera);
				
					this.setResponsePage(new AccionesSobreElMapaPage(tablero));
				
			}
        	
        });
    	
    	
    	
    	
    	this.add(form);
       
    	
    	
    }
}

